package com.kakarote.hrm.controller;


import com.kakarote.core.common.Result;
import com.kakarote.hrm.constant.EmployeeFileType;
import com.kakarote.hrm.entity.BO.*;
import com.kakarote.hrm.entity.PO.HrmEmployee;
import com.kakarote.hrm.entity.PO.HrmEmployeeFile;
import com.kakarote.hrm.service.IHrmEmployeeFileService;
import com.kakarote.hrm.service.IHrmEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 员工附件表 前端控制器
 * </p>
 *
 * @author huangmingbo
 * @since 2020-05-12
 */
@RestController
@RequestMapping("/hrmEmployeeFile")
@Api(tags = "员工管理-员工附件")
public class HrmEmployeeFileController {

    @Autowired
    private IHrmEmployeeFileService employeeFileService;

    @Autowired
    private IHrmEmployeeService employeeService;

    @PostMapping("/queryFileNum/{employeeId}")
    @ApiOperation("查询员工总体附件")
    public Result<Map<String, Object>> queryFileNum(@PathVariable("employeeId") Long employeeId) {
        Map<String, Object> map = employeeFileService.queryFileNum(employeeId);
        return Result.ok(map);
    }

    @PostMapping("/queryFileBySubType")
    @ApiOperation("根据附件类型查询附件详情")
    public Result<List<HrmEmployeeFile>> queryFileBySubType(@RequestBody QueryFileBySubTypeBO queryFileBySubTypeBO) {
        List<HrmEmployeeFile> list = employeeFileService.queryFileBySubType(queryFileBySubTypeBO);
        return Result.ok(list);
    }

    @PostMapping("/addFile")
    @ApiOperation("添加附件")
    public Result addFile(@RequestBody AddFileBO addFileBO) {
        if (addFileBO.getSubType() == EmployeeFileType.PERSONAL_ID_PHOTO.getValue()){
            List<HrmEmployeeFile> list = employeeFileService.lambdaQuery().eq(HrmEmployeeFile::getEmployeeId, addFileBO.getEmployeeId()).eq(HrmEmployeeFile::getSubType, addFileBO.getSubType()).list();
            if (!list.isEmpty()){
                return Result.error(400, "该员工已存在个人证件照");
            }
        }
        employeeFileService.addFile(addFileBO);
        return Result.ok();
    }

    /**
     * 保存个人证件照
     * @param saveIdPhotoBO
     * @return
     */
    @PostMapping("/saveIdPhoto")
    @ApiOperation("保存个人证件照")
    public Result saveIdPhoto(@RequestBody SaveIdPhotoBO saveIdPhotoBO) {
        HrmEmployee employee = employeeService.lambdaQuery().eq(HrmEmployee::getMobile, saveIdPhotoBO.getMobile()).eq(HrmEmployee::getIsDel, 0).one();
        if (employee != null){
            HrmEmployeeFile hrmEmployeeFile = new HrmEmployeeFile();
            hrmEmployeeFile.setEmployeeId(employee.getEmployeeId());
            hrmEmployeeFile.setSubType(EmployeeFileType.PERSONAL_ID_PHOTO.getValue());
            employeeFileService.deleteIdPhotoFile(hrmEmployeeFile);
            AddFileBO addFileBO = new AddFileBO();
            addFileBO.setEmployeeId(employee.getEmployeeId());
            addFileBO.setSubType(saveIdPhotoBO.getSubType());
            addFileBO.setFileId(saveIdPhotoBO.getFileId());
            employeeFileService.addFile(addFileBO);
        }
        return Result.ok();
    }


    @PostMapping("/deleteFile/{employeeFileId}")
    @ApiOperation("删除附件")
    public Result deleteFile(@PathVariable("employeeFileId") Long employeeFileId) {
        employeeFileService.deleteFile(employeeFileId);
        return Result.ok();
    }

    @PostMapping("/materialExportZip")
    @ApiOperation("附件材料压缩包加密导出")
    public void materialExportZip(@RequestBody ExportEmployeeMaterialBO employeeMaterialBO, HttpServletResponse response) {
        employeeFileService.materialExportZip(response, employeeMaterialBO.getEmployeeId(), employeeMaterialBO.getTypeList());
    }

    @PostMapping("/fileExportZip")
    @ApiOperation("附件导出")
    public void fileExportZip(@RequestBody ExportEmployeeFileBO exportEmployeeFileBO, HttpServletResponse response) {
        employeeFileService.fileExportZip(response, exportEmployeeFileBO.getFileIdList());
    }
}

